<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateOrderGoodsRequest;
use App\Http\Requests\UpdateOrderGoodsRequest;
use App\Repositories\OrderGoodsRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class OrderGoodsController extends AppBaseController
{
    /** @var  OrderGoodsRepository */
    private $orderGoodsRepository;

    public function __construct(OrderGoodsRepository $orderGoodsRepo)
    {
        $this->orderGoodsRepository = $orderGoodsRepo;
    }

    /**
     * Display a listing of the OrderGoods.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->orderGoodsRepository->pushCriteria(new RequestCriteria($request));
        $orderGoods = $this->orderGoodsRepository->paginate(20);

        return view('order_goods.index')
            ->with('orderGoods', $orderGoods);
    }

    /**
     * Show the form for creating a new OrderGoods.
     *
     * @return Response
     */
    public function create()
    {
        return view('order_goods.create');
    }

    /**
     * Store a newly created OrderGoods in storage.
     *
     * @param CreateOrderGoodsRequest $request
     *
     * @return Response
     */
    public function store(CreateOrderGoodsRequest $request)
    {
        $input = $request->all();

        $orderGoods = $this->orderGoodsRepository->create($input);

        Flash::success('Order Goods saved successfully.');

        return redirect(route('orderGoods.index'));
    }

    /**
     * Display the specified OrderGoods.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            Flash::error('Order Goods not found');

            return redirect(route('orderGoods.index'));
        }

        return view('order_goods.show')->with('orderGoods', $orderGoods);
    }

    /**
     * Show the form for editing the specified OrderGoods.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            Flash::error('Order Goods not found');

            return redirect(route('orderGoods.index'));
        }

        return view('order_goods.edit')->with('orderGoods', $orderGoods);
    }

    /**
     * Update the specified OrderGoods in storage.
     *
     * @param  int              $id
     * @param UpdateOrderGoodsRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateOrderGoodsRequest $request)
    {
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            Flash::error('Order Goods not found');

            return redirect(route('orderGoods.index'));
        }

        $orderGoods = $this->orderGoodsRepository->update($request->all(), $id);

        Flash::success('Order Goods updated successfully.');

        return redirect(route('orderGoods.index'));
    }

    /**
     * Remove the specified OrderGoods from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $orderGoods = $this->orderGoodsRepository->findWithoutFail($id);

        if (empty($orderGoods)) {
            Flash::error('Order Goods not found');

            return redirect(route('orderGoods.index'));
        }

        $this->orderGoodsRepository->delete($id);

        Flash::success('Order Goods deleted successfully.');

        return redirect(route('orderGoods.index'));
    }
}
